<template>
  <div class="login-container">
    <div class="login_box">
      <!-- 头像去 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFromRef"
        label-width="0px"
        :model="loginForm"
        class="login_form"
        :rules="loginFromRules"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-s-custom"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="el-icon-key"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetloginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "myLogin",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      // 表单验证规则
      loginFromRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 9, message: "长度在3~9个字符", trigger: "blur" },
        ],
        password: [
          {
            required: true,
            min: 3,
            max: 9,
            message: "长度在3~9个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetloginFrom() {
      this.$refs.loginFromRef.resetFields();
    },
    login() {
      this.$refs.loginFromRef.validate(async (valid) => {
        if (!valid) return alert("您输入的用户名或密码不合法！！！");
        const { data } = await this.$axios.post("login", this.loginForm);
        // console.log(data.data.token);
       if(data.meta.status !== 200) return this.$message.error('登录失败 账号或密码错误！')
       this.$message.success('登录成功')

        // 将登录成功之后的token保存到 sessionStorage中 
        sessionStorage.setItem('token',data.data.token)

        this.$router.push('/home')
      });
    },
  },
};
</script>

<style lang="less" scoped>
html {
  height: 2000px;
  
}

.login-container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #ffffff;
  
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    position: relative;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #cfe2d0;
    border: 1px solid #111;
    border-radius: 50%;
    height: 130px;
    width: 130px;
    padding: 10px;
    box-shadow: 0 0 10px #000;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #86bab1;
    }
  }
}
.login_form {
  position: absolute;
  width: 100%;
  bottom: 0;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>